<?php

namespace app\admin\controller\login;

use app\admin\controller\AdminController;
use tmcore\services\CaptchaService;
use app\admin\validate\login\CaptchaValidate;
use think\response\Json;

class Captcha extends AdminController
{
    /**
     * 创建验证码
     * @return Json
     */
    public function create()
    {
        $params = (new CaptchaValidate())->checkParam('create');
        return $this->success((new CaptchaService($params['captcha_type']))->create());
    }

    /**
     * 一次校验验证码
     * @return Json
     */
    public function check()
    {
        $params = (new CaptchaValidate())->post()->checkParam('check');
        $captchaService = new CaptchaService($params['captcha_type']);
        $captchaService->check($params['captcha_key'], $params['captcha_code']);
        return $this->success();
    }

    /**
     * 二次校验验证码
     * @return Json
     */
    public function verification()
    {
        $params = (new CaptchaValidate())->post()->checkParam('verification');
        $captchaService = new CaptchaService($params['captcha_type']);
        $captchaService->verification($params['captcha_code']);
        return $this->success();
    }

    /**
     * 创建验证码图片
     * @return Json
     */
    public function codeCreate()
    {
        return $this->success(CaptchaService::codeCreate());
    }
}